package Runde4;

public class Test {
	public static void main (String[] args) {

		newRPT("");
		ReplaceableTree rpt = newRPT("a");
		System.out.println("Fuege Knoten b, c, d, e, f hinzu ...");
		rpt.add("b");
		rpt.add("c");
		rpt.add("d");
		rpt.add("e");
		rpt.add("f");
		System.out.println("Printe ReplaceableTree aus ...");
		System.out.println(rpt);
		strSearch(rpt,"b");
		strSearch(rpt,"e");
		strSearch(rpt,"a");
		strSearch(rpt,"z");
		strSearch(rpt,null);
		strContains(rpt,"a");
		strContains(rpt,"z");
		
		System.out.println();
		System.out.println("Erstelle falschen Baum..");
		System.out.println("r\n  * s\n    * t\n  *u");
		System.out.println("Ersetze f mit falschem Baum..");
		rpt.replace(strSearch(rpt,"f"), "r\n  * s\n    * t\n  *u");
		System.out.println("Printe ReplaceableTree aus ...");
		System.out.println(rpt);
		
		ReplaceableTree rpt2 = newRPT("z");
		System.out.println();
		System.out.println("Fuege Knoten y, x, w, v, u hinzu ...");
		rpt2.add("y");
		rpt2.add("x");
		rpt2.add("w");
		rpt2.add("v");
		rpt2.add("u");
		System.out.println("Printe ReplaceableTree aus ...");
		System.out.println(rpt2);
		
		System.out.println("Erstelle falschen Node..");
		System.out.println("left right left right");
		System.out.println("Ersetze falsche Node mit subTree mit Wurzel z");
		rpt.replace("left right left right", rpt2.toString());
		System.out.println("Printe ReplaceableTree aus ...");
		System.out.println(rpt2);
		System.out.println("Ersetze d mit subTree mit Wurzel z");
		rpt.replace(strSearch(rpt,"d"), rpt2.toString());
		System.out.println("Printe ReplaceableTree aus ...");
		System.out.println(rpt);
		
		IntTree it = newIT(1);
		System.out.println("Fuege Knoten -2, 3, -4, 5 hinzu ...");
		it.add(-2);
		it.add(3);
		it.add(-4);
		it.add(5);
		System.out.println("Printe IntTree aus ...");
		System.out.println(it);
		intSearch(it,2);
		intSearch(it,-2);
		intContains(it,6);
		intContains(it,-4);
		
		System.out.println();
		IntTree it2 = newIT(-10);
		System.out.println("Fuege Knoten 9, 8, 7, 6 hinzu ...");
		it2.add(9);
		it2.add(8);
		it2.add(7);
		it2.add(6);
		System.out.println("Printe IntTree aus ...");
		System.out.println(it2);
		
		System.out.println("Ersetze 5 mit subTree mit Wurzel -10");
		it.replace(intSearch(it,5), it2.toString());
		System.out.println("Printe IntTree aus ...");
		System.out.println(it);
		
		PreorderTree pt = newPreTr("z");
		System.out.println("Fuege in dieser Reihenfolge einen falschen, dann Knoten o, r, p hinzu ...");
		pt.add("");
		pt.add("o");
		pt.add("r");
		pt.add("p");
		System.out.println("Gebe den Baum aus...");
		System.out.print(pt);
		
		System.out.println("Und nun selbiges mit den zwei anderen Baumarten und danach die Traversierungen:");
		
		InorderTree intr = newInTr("z");
		intr.add("");
		intr.add("o");
		intr.add("r");
		intr.add("p");
		
		PostorderTree postr = newPostTr("z");
		postr.add("");
		postr.add("o");
		postr.add("r");
		postr.add("p");
		
		System.out.print("Preorder: ");
		System.out.println(pt.traverse());
		System.out.print("Inorder: ");
		System.out.println(intr.traverse());
		System.out.print("Postorder: ");
		System.out.println(postr.traverse());
		
		System.out.println();
		
		sortedContains(pt, "a");
		sortedContains(intr, "z");
		
		sortedSearch(postr, "p");

	}
	
	public static ReplaceableTree newRPT(String root) {
		System.out.print("Istanziere neuen ReplaceableTree mit Wurzel \"" + root + "\": ");
		try {
			ReplaceableTree temp = new ReplaceableTree(root);
			System.out.print("erfolg.\n");
			return temp;
		} catch (InvalidParamException ex) {
			System.out.print(ex + "\n");
			return null;
		}
	}
	
	public static String strSearch(StringTree tree, String node) {
		System.out.print(tree.getClass().getSimpleName() + ".search(" + node + ") Ergebnis: ");
		try {
			System.out.print(tree.search(node) + "\n");
			return tree.search(node);
		} catch (InvalidParamException ex) {
			System.out.print(ex + "\n");
		}
		return "";
	}
	
	public static void strContains(StringTree tree, String node) {
		System.out.print(tree.getClass().getSimpleName() + ".contains(" + node + ") Ergebnis: ");
		try {
			System.out.print(tree.contains(node) + "\n");
		} catch (InvalidParamException ex) {
			System.out.print(ex + "\n");
		}
	}
	
	public static IntTree newIT(int root) {
		System.out.print("Instanziere neuen IntTree mit Wurzel " + root + ": ");
		try {
			IntTree temp = new IntTree(root);
			System.out.print("erfolg.\n");
			return temp;
		} catch (InvalidParamException ex) {
			System.out.print(ex + "\n");
			return null;
		}
	}
	
	public static String intSearch(IntTree tree, int node) {
		System.out.print(tree.getClass().getSimpleName() + ".search(" + node + ") Ergebnis: ");
		try {
			System.out.print(tree.search(node) + "\n");
			return tree.search(node);
		} catch (InvalidParamException ex) {
			System.out.print(ex + "\n");
		}
		return "";
	}
	
	public static void intContains(IntTree tree, int node) {
		System.out.print(tree.getClass().getSimpleName() + ".contains(" + node + ") Ergebnis: ");
		try {
			System.out.print(tree.contains(node) + "\n");
		} catch (InvalidParamException ex) {
			System.out.print(ex + "\n");
		}
	}
	
	public static InorderTree newInTr(String root) {
		System.out.print("Instanziere neuen InorderTree mit Wurzel " + root + ": ");
		try {
			InorderTree temp = new InorderTree(root);
			System.out.print("erfolg.\n");
			return temp;
		} catch (InvalidParamException ex) {
			System.out.print(ex + "\n");
			return null;
		}
	}
	
	public static PreorderTree newPreTr(String root) {
		System.out.print("Instanziere neuen PreorderTree mit Wurzel " + root + ": ");
		try {
			PreorderTree temp = new PreorderTree(root);
			System.out.print("erfolg.\n");
			return temp;
		} catch (InvalidParamException ex) {
			System.out.print(ex + "\n");
			return null;
		}
	}
	
	public static PostorderTree newPostTr(String root) {
		System.out.print("Instanziere neuen PostorderTree mit Wurzel " + root + ": ");
		try {
			PostorderTree temp = new PostorderTree(root);
			System.out.print("erfolg.\n");
			return temp;
		} catch (InvalidParamException ex) {
			System.out.print(ex + "\n");
			return null;
		}
	}
	
	public static String sortedSearch(SortedTree tree, String node) {
		System.out.print(tree.getClass().getSimpleName() + ".search(" + node + ") Ergebnis: ");
		try {
			System.out.print(tree.search(node) + "\n");
			return tree.search(node);
		} catch (InvalidParamException ex) {
			System.out.print(ex + "\n");
		}
		return "";
	}
	
	public static void sortedContains(SortedTree tree, String node) {
		System.out.print(tree.getClass().getSimpleName() + ".contains(" + node + ") Ergebnis: ");
		try {
			System.out.print(tree.contains(node) + "\n");
		} catch (InvalidParamException ex) {
			System.out.print(ex + "\n");
		}
	}
}
